package com.rich.web.servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;

import com.rich.constant.Constant;
import com.rich.domain.User;
import com.rich.service.UserService;
import com.rich.service.impl.UserServiceImpl;
import com.rich.utils.BeanFactory;
import com.rich.utils.UUIDUtils;

/**
 * 用户模块
 * Servlet implementation class UserServlet
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	UserService us = (UserService) BeanFactory.getBean("UserService");
	/**
	 * 用户激活
	 * @throws Exception 
	 */
	public String active(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//接受code
		String code = request.getParameter("code");
		
		User user = us.active(code);
		
		if(user == null) {
			//没有找到激活用户
			request.setAttribute("msg", "请重新激活或注册！");
			return "/jsp/msg.jsp";
		}
		request.setAttribute("msg", "恭喜您激活成功，请登录！");
		return "/jsp/msg.jsp";
	}
	
	/**
	 * 跳转到注册页面
	 * @param request
	 * @param response
	 * @return
	 */
	public String registerUI(HttpServletRequest request,HttpServletResponse response) {
		return "/jsp/register.jsp";
	}
	

	/**
	 * 用户注册
	 * @param request
	 * @param response
	 * @return
	 */
	public String regist(HttpServletRequest request, HttpServletResponse response) {
		try {
			//封装对象
			User user = new User();
			BeanUtils.populate(user, request.getParameterMap());
			//由于部分属性在注册页面没有，需手动封装
			//封装uid
			user.setUid(UUIDUtils.getId());
			//封装激活状态state
			user.setState(Constant.USER_IS_NOT_ACTIVE);
			//封装激活码code
			user.setCode(UUIDUtils.getCode());
			//调用service完成注册
			us.regist(user);
			//页面转发提示信息
			request.setAttribute("msg", "恭喜您,注册成功，请登陆邮箱完成激活!");
		} catch (Exception e) {
			request.setAttribute("msg", "用户注册失败");
			return "/jsp/msg.jsp";
		}
		return "/jsp/msg.jsp";
	}
	
	/**
	 * 跳转到登陆页面
	 * @param request
	 * @param response
	 * @return
	 */
	public String loginUI(HttpServletRequest request,HttpServletResponse response) {
		return "/jsp/login.jsp";
	}
	
	/**
	 * 用户登陆
	 * @throws Exception 
	 */
	public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			//获取用户名密码
			String userName = request.getParameter("username");
			String password = request.getParameter("password");
			//调用servce完成登陆 返回user
			User user = us.login(userName,password);
			
			if(user == null) {
				//用户不匹配
				request.setAttribute("msg", "用户名不匹配！");
				return "/jsp/login.jsp";
			}
			//不为空继续判断是否激活
			if(Constant.USER_IS_ACTIVE != user.getState()) {
				request.setAttribute("msg", "请先激活再登陆！");
				return "/jsp/msg.jsp";
			}
			HttpSession session = request.getSession();
			session.setMaxInactiveInterval(360*24*60*60*60);
			session.setAttribute("user", user);
		
			//当用户勾选了记住用户名，放到cookie中
			if(Constant.SAVE_NAME.equals(request.getParameter("saveName"))) {
				Cookie cookie = new Cookie("userName", URLEncoder.encode(userName, "UTF-8"));
				cookie.setMaxAge(Integer.MAX_VALUE);
				cookie.setPath(request.getContextPath()+"/");
				response.addCookie(cookie);
			}
			//跳转到首页
			response.sendRedirect(request.getContextPath()+"/index");
		} catch (Exception e) {
			request.setAttribute("msg", "用户登陆失败,请重新登陆！");
			return "/jsp/msg.jsp";
		}
		return null;
	}
	
	/**
	 * 用户退出
	 */
	public String logout(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.getSession().removeAttribute("user");;
			response.sendRedirect(request.getContextPath()+"/index");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
